package com.buzz.activity;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.buzz.service.BackgroundService;
import com.buzz.utils.GlobalConst;

import java.util.Properties;

public class LoginActivity extends ActionBarActivity {

    MyApplication myApp;
    LoginReceiver loginReceiver;
    IntentFilter intentFilter;
    long firstTime = 0;
    private ProgressDialog progressDialog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        myApp = (MyApplication) getApplication();

        loginReceiver = new LoginReceiver();
        intentFilter = new IntentFilter();
        intentFilter.addAction(GlobalConst.ACTION_USER_LOGIN);
        registerReceiver(loginReceiver, intentFilter);

        final Button btn_sign = (Button) findViewById(R.id.login_activity_btn_sign);
        final EditText et_email = (EditText) findViewById(R.id.login_activity_et_email);
        final EditText et_password = (EditText) findViewById(R.id.login_activity_et_password);

        //for testing
        //et_email.setText("nick.chung");
        //et_password.setText("123456");

        Intent it = getIntent();
        if (it.hasExtra("email")) {
            et_email.setText(it.getStringExtra("email"));
        }

        if (it.hasExtra("pwd")) {
            et_password.setText(it.getStringExtra("pwd"));
        }

        btn_sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(LoginActivity.this, SignActivity.class);
                startActivity(it);
                LoginActivity.this.finish();
            }
        });


        Button btnLogin = (Button) findViewById(R.id.login_activity_btn_login);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                progressDialog = new ProgressDialog(LoginActivity.this);
                //设置进度条风格，风格为圆形，旋转的
                progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                progressDialog.setTitle("提示！");
                progressDialog.setMessage(getString(R.string.Landing));
                // 设置ProgressDialog 的进度条是否不明确
                progressDialog.setIndeterminate(false);
                progressDialog.show();
                String email = et_email.getText().toString();
                String password = et_password.getText().toString();

                if (email.isEmpty()) {
                    Toast.makeText(getApplicationContext(), getString(R.string.msg_check_email), Toast.LENGTH_LONG).show();
                    return;
                }
                if (password.isEmpty()) {
                    Toast.makeText(getApplicationContext(), getString(R.string.msg_check_pwd), Toast.LENGTH_LONG).show();
                    return;

                }

                if (myApp.serverReady) {
                    myApp.socketHelper.Send(String.format(GlobalConst.CMD_GET_USER_INFO, email, password));
                } else {
                    Toast.makeText(getApplicationContext(), getString(R.string.msg_check_server), Toast.LENGTH_LONG).show();
                }
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(loginReceiver);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            long secondTime = System.currentTimeMillis();
            if (secondTime - firstTime > GlobalConst.SYSTEM_EXIT_INTERVAL) {
                Toast.makeText(LoginActivity.this, getString(R.string.msg_quit_system), Toast.LENGTH_SHORT).show();
                firstTime = System.currentTimeMillis();
                return true;
            } else {
                Intent intent = new Intent(this, BackgroundService.class);
                stopService(intent);
                this.finish();
                System.exit(0);
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    public class LoginReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                if (myApp.logonUser.isLogon) {
                    startActivity(new Intent(LoginActivity.this, ExhibitionActivity.class));
                    //启动服务
                    Intent intent_back = new Intent(LoginActivity.this, BackgroundService.class);
                    startService(intent_back);
                    Intent intent_music = new Intent(LoginActivity.this, MusicPlayService.class);
                    startService(intent_music);
                    LoginActivity.this.finish();
                } else {
                    Toast.makeText(getApplicationContext(), getString(R.string.msg_login_failed), Toast.LENGTH_LONG).show();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
